//
//  MXHomeShowPhotoVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/12.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 照片墙
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXHomeShowPhotoVC.h"
#import "AFNetworkHandler.h"
#import "MXHomeShowPhotoModel.h"
#import "MXHomeShowPhotoTableViewCell.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
@interface MXHomeShowPhotoVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,retain) MBProgressHUD *HUD;
@property (nonatomic, retain) UIView *alterView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *HostArray;
@property (nonatomic, assign) NSInteger pageNum;

@end

@implementation MXHomeShowPhotoVC
- (void)dealloc
{
    [_HUD release];
    [_alterView release];
    [_HostArray release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.HostArray = [NSMutableArray array];
    
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 64)];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.text = @"";
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.userInteractionEnabled = YES;
        [self.view addSubview:titleLabel];
        
        UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        backButton.frame = CGRectMake(WIDTHPRO * 10,HEIGHTPRO * 29,WIDTHPRO * 30,HEIGHTPRO * 30);
        [backButton setBackgroundImage:[UIImage imageNamed:@"backButton_blue"] forState:(UIControlStateNormal)];
        [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
        [titleLabel addSubview:backButton];
        
        UILabel *titleWord = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - WIDTHPRO * 60,HEIGHTPRO * 24,WIDTHPRO * 120,HEIGHTPRO * 40)];
        titleWord.text = @"照片墙";
        titleWord.textAlignment = NSTextAlignmentCenter;
        titleWord.font = [UIFont systemFontOfSize:18];
        [titleWord setTextColor:[UIColor redColor]];
        [titleLabel addSubview:titleWord];
    }

    [self creatTableView];
    
    self.pageNum = 2;
//    [self creatTableView];
    
    [self GetData];
    // 添加刷新方法
    [self loadingCollectionView];
    
    [self refreshTableView];
    // Do any additional setup after loading the view.
}
-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}
- (void)creatTableView
{
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, WIDTHPRO * 64, WIDTH, HEIGHT - HEIGHTPRO * 64) style:(UITableViewStylePlain)] autorelease];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self uiActivityindicatorviewStart];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(uiActivityindicatorviewEnd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

#pragma mark -- 下拉刷新
-(void)refreshTableView
{
    //    __block MXFindVC *findVC = self;
    [self.tableView addHeaderWithCallback:^{
        //        [self GetFindData];
        [self.tableView headerEndRefreshing];
    }];
    //添加下拉刷新头部控件
    
    //    [self.collectionView reloadData];
}

#pragma mark -- 上拉加载

-(void)loadingCollectionView
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSString *ShowPhotoPath = [NSString stringWithFormat:@"http://api.nanyibang.com/haokanme?hkm_sign=b9d086d7939403d6a201238fbc35700e&member_id=15243&member_type=member&page=%ld&public=1&random_key=38388&version=1.2.7", (long)self.pageNum];
            self.pageNum += 1;
            NSLog(@"%@", ShowPhotoPath);
            [AFNetworkHandler GETwithAFNByURL:ShowPhotoPath completion:^(id result) {
                NSLog(@"result = %@", result);
                
                [self.HostArray removeAllObjects];
                
                NSArray *dataArr = [result objectForKey:@"data"];
                for (NSDictionary *dict in dataArr) {
                    MXHomeShowPhotoModel *showModel = [[[MXHomeShowPhotoModel alloc] initWithDic:dict] autorelease];
                    [self.HostArray addObject:showModel];
                }
                [self.tableView reloadData];
                [self uiActivityindicatorviewEnd];
            }];

            
            [self.tableView reloadData];
            [self.tableView footerEndRefreshing];
        });
    }];
}


- (void)GetData
{
    NSString *ShowPhotoPath = @"http://api.nanyibang.com/haokanme?hkm_sign=b9d086d7939403d6a201238fbc35700e&member_id=15243&member_type=member&page=1&public=1&random_key=38388&version=1.2.7";
    NSLog(@"%@", ShowPhotoPath);
    [AFNetworkHandler GETwithAFNByURL:ShowPhotoPath completion:^(id result) {
        NSLog(@"result = %@", result);
        
        [self.HostArray removeAllObjects];
        
        NSArray *dataArr = [result objectForKey:@"data"];
        for (NSDictionary *dict in dataArr) {
            MXHomeShowPhotoModel *showModel = [[[MXHomeShowPhotoModel alloc] initWithDic:dict] autorelease];
            [self.HostArray addObject:showModel];
        }
        [self.tableView reloadData];
        [self uiActivityindicatorviewEnd];
    }];
}
/************************************ TableView 协议部分 **********************************/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.HostArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeShowPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[MXHomeShowPhotoTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"resue"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell GetShowPhotoInfo:self.HostArray[indexPath.section]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MXHomeShowPhotoModel *model = self.HostArray[indexPath.row];
    
    if ([model.comment isEqualToString:@""]) {
        return HEIGHTPRO * 500;
    } else {
        return HEIGHTPRO * 500;
        
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
